All files / app/[slug] page.tsx

96.87% Statements 31/32
73.91% Branches 17/23
100% Functions 4/4
96.66% Lines 29/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                    1x 2x               3x   3x 3x     3x 3x 3x   3x 1x             2x 2x     3x     3x       3x 3x       2x 2x   2x                         3x       3x                   2x 2x 2x 2x   2x 2x   2x   1x    
 
import { draftMode } from "next/headers";
import { getAllPagesSlugs, getPageBySlug } from "@/data/loaders";
import { BlockRenderer } from "@/components/block-renderer";
import { generateMetadataObject } from "@/lib/metadata"; // ✅ SEO Helper
import { Metadata } from "next";
import { strapiImage } from '@/lib/strapi/strapiImage';
import { notFound } from "next/navigation";
 
export async function generateStaticParams() {
  const pages = await getAllPagesSlugs();
  return pages.data.map((page) => ({
    slug: page.slug,
  }));
}
 
 
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> {
 
  const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000";
 
  const resolveParams = await params;
  const slug = await resolveParams?.slug;
 
  // Fetch page data for the given slug
  const { isEnabled: isDraftMode } = await draftMode();
  const status = isDraftMode ? "draft" : "published";
  const data = await getPageBySlug(slug, status);
 
  if (!data?.data?.length) {
    return {
      title: "Page Not Found | Bitmutex Technologies",
      description: "The requested page does not exist. Browse more content by Bitmutex Technologies.",
      robots: "noindex, nofollow",
    };
  }
 
  const pageData = data.data[0]; // Extract the page data
  const seo = pageData?.seo;
 
  //  Generate metadata using the helper function
  const metadata = generateMetadataObject(seo);
 
  //  Ensure fallback values if SEO fields are missing
  const title = seo?.metaTitle
    ? `${seo.metaTitle} | Bitmutex`
    : `${pageData.title || "Untitled"} | Bitmutex`;
 
  let description = seo?.metaDescription || pageData?.description || "Explore insightful content on Bitmutex.";
  Iif (description.length > 150) {
    description = description.substring(0, description.lastIndexOf(" ", 150)) + "...";
  }
 
  metadata.title = title;
  metadata.description = description;
 
  metadata.openGraph = {
    ...(metadata.openGraph as any),
    title,
    description,
    images: seo?.metaImage 
    ? [{ url: strapiImage(seo?.metaImage.url) }] 
    : [{ url: `${BASE_URL}/pages-fallback.png`}],
    url: `${BASE_URL}/${slug}`, //  Dynamic URL
    site_name: "Bitmutex",
    locale: "en_US",
    type: "website",
  };
 
  metadata.alternates = {
    canonical: `${BASE_URL}/${slug}`,
  };
 
  return metadata;
}
 
 
interface PageProps {
  params: Promise<{ slug: string }>
}
 
 
export default async function PageBySlugRoute({ params }: PageProps) {
  const resolveParams = await params;
  const slug = await resolveParams?.slug;
  const { isEnabled: isDraftMode } = await draftMode();
  const status = isDraftMode ? "draft" : "published";
 
  const data = await getPageBySlug(slug, status);
  const blocks = data?.data[0]?.blocks;
 
  if (!blocks) notFound(); // Trigger Next.js 404 page
 
  return <div>{blocks ? <BlockRenderer blocks={blocks} /> : null}</div>;
}